<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="refund_user_id" label="退款用户" required>
        <uni-easyinput v-model="formData.refund_user_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="refund_from_transaction_id" label="支付方平台订单号" required>
        <uni-easyinput v-model="formData.refund_from_transaction_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="refund_order_id" label="退款单号" required>
        <uni-easyinput v-model="formData.refund_order_id" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="refund_total_amount" label="退款金额" required>
        <uni-easyinput placeholder="单位必须是分" type="number" v-model="formData.refund_total_amount"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="order_total_amount" label="订单总金额" required>
        <uni-easyinput placeholder="单位必须是分" type="number" v-model="formData.order_total_amount"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="refund_remark" label="退款备注">
        <uni-easyinput v-model="formData.refund_remark" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="operator_id" label="操作人员">
        <uni-data-checkbox :multiple="true" v-model="formData.operator_id"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="pay_platform" label="支付平台">
        <uni-data-checkbox v-model="formData.pay_platform" :localdata="formOptions.pay_platform_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="refund_status" label="退款状态">
        <uni-data-checkbox v-model="formData.refund_status" :localdata="formOptions.refund_status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="refund_module" label="退款模块">
        <uni-data-checkbox v-model="formData.refund_module" :localdata="formOptions.refund_module_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/admin-pay-refund.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'admin-pay-refund';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "refund_user_id": "",
        "refund_from_transaction_id": "",
        "refund_order_id": "",
        "refund_total_amount": null,
        "order_total_amount": null,
        "refund_remark": "",
        "operator_id": [],
        "pay_platform": 0,
        "refund_status": 0,
        "refund_module": "food-order-list"
      }
      return {
        formData,
        formOptions: {
          "pay_platform_localdata": [
            {
              "value": 0,
              "text": "微信支付"
            }
          ],
          "refund_status_localdata": [
            {
              "value": 0,
              "text": "正在退款"
            },
            {
              "value": 1,
              "text": "退款成功"
            },
            {
              "value": 2,
              "text": "退款异常"
            },
            {
              "value": 3,
              "text": "退款关闭"
            }
          ],
          "refund_module_localdata": [
            {
              "value": "food-order-list",
              "text": "美食外卖"
            },
            {
              "value": "admin-order-list",
              "text": "快递代取"
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
